package com.pdsu.graduation_design.views;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.adapter.MyLikeCommodityAdapter;
import com.pdsu.graduation_design.dto.CommodityLikeDTO;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class My_Like_Commodity_Fragment extends Fragment {
    private View currentView;
    private Context context;
    private ListView my_like_co_lv;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        currentView=inflater.inflate(R.layout.my_like_co_fragment,null);

        /**
         * 获取数据
         */
        //数据
        init();
        bind();
        return currentView;
    }
    void init(){
        context=getContext();
        my_like_co_lv=currentView.findViewById(R.id.my_like_co_lv);
    }
    void bind(){

        RequestParams requestParams=new RequestParams(MainActivity.URL+"/commodity/getLikeCommodity");
        requestParams.addParameter("uid",MainActivity.user_admin.getUid());
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    Result r= JsonUtil.JsonToObj(result,Result.class);

                    if (r.getCode().equals("200")){

                        List<CommodityLikeDTO> commodityLikeDTOS=JsonUtil.json2list(r.getData().toString(),CommodityLikeDTO.class);
                        my_like_co_lv.setAdapter(new MyLikeCommodityAdapter(context,commodityLikeDTOS));
                        my_like_co_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent=new Intent(context,MainListViewItemDetailActivity.class);
                                intent.putExtra("cid",commodityLikeDTOS.get(position).getCid());
                                startActivity(intent);

                            }
                        });



                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
